#!/usr/bin/env bash

__rsdk_build() {
	local product

	radiolist_init
	local products=() i
	mapfile -t products < <(jq -er '.[].product' "$SCRIPT_DIR/../../share/rsdk/configs/products.json") && array_remove "products" ""
	for i in "${products[@]}"; do
		radiolist_add "$i" "OFF"
	done
	if ! radiolist_show "Please select a product:" || ((${#RTUI_RADIOLIST_STATE_NEW[@]} == 0)); then
		return
	fi
	product="$(radiolist_getitem "${RTUI_RADIOLIST_STATE_NEW[0]}")"

	if ! yesno "Are you sure to build for '$product'?"; then
		return
	fi

	rsdk build "$product"
}

__rsdk_about() {
	msgbox "rsdk - RadxaOS Software Development Kit

Copyright 2024-$(date +%Y) Radxa Computer Co., Ltd"
}

tui_main() {
	menu_init
	menu_add __rsdk_build "Build system image"
	menu_add_separator
	menu_add __rsdk_about "About"
	menu_show "Please select a task:"
}

main() {
	local SCRIPT_DIR
	SCRIPT_DIR="$(dirname "$(realpath "$0")")"
	if [[ -f "$SCRIPT_DIR/../../lib/librtui/tui.sh" ]]; then
		# shellcheck source=/dev/null
		source "$SCRIPT_DIR/../../lib/librtui/tui.sh"
	elif [[ -f "$SCRIPT_DIR/../../../externals/librtui/src/lib/librtui/tui.sh" ]]; then
		# shellcheck source=externals/librtui/src/lib/librtui/tui.sh
		source "$SCRIPT_DIR/../../../externals/librtui/src/lib/librtui/tui.sh"
	elif [[ -f "/usr/lib/librtui/tui.sh" ]]; then
		# shellcheck source=/dev/null
		source "/usr/lib/librtui/tui.sh"
	else
		echo "Missing librtui. Please make sure you have installed it." >&2
		return 1
	fi
	# shellcheck source=src/lib/rsdk/utils.sh
	source "$SCRIPT_DIR/../../lib/rsdk/utils.sh"
	# shellcheck source=src/lib/rsdk/stdlib.sh
	source "$SCRIPT_DIR/../../lib/rsdk/stdlib.sh"

	local DEBUG="${DEBUG:-false}"

	tui_start tui_main "RSDK"
}

main "$@"
